package com.ww.basic.oop

import scala.collection.mutable

class Marker3 private (val color: String) {
  override def toString = s"marker color $color"
}

object Marker3 {
  private val markers = mutable.Map(
    "red" -> new Marker3("red"),
    "blue" -> new Marker3("blue"),
    "yellow" -> new Marker3("yellow")
  )

  /**
   * 如果方法不接收任何参数，方法定义中的括号可以不写
   * @return
   */
  def supportedColors: Iterable[String] = markers.keys

  /**
   * 伴生对象可以不用new关键字就可以创建伴生类的实例，特殊的apply()方法就达到这种效果
   * @param color
   * @return
   */
  def apply(color: String): Marker3 = markers.getOrElseUpdate(color, op = new Marker3(color))
}

object TestMarker3 {
  def main(args: Array[String]): Unit = {
    println(s"Supported colors are: ${Marker3.supportedColors}")
    println(Marker3("blue"))
    println(Marker3("red"))
  }
}